import axios from 'axios'
import Vue from 'vue'

const instance = axios.create({
  baseURL: '/',
  timeout: 5000,
  headers: {
    'Content-Type': 'application/json'
  }
})

instance.interceptors.request.use(config => {
  // 对请求进行处理，例如添加 token 等
  config.headers = Object.assign({ authorization:'Bear ' + localStorage.token },config.headers || {}) 
  config.data = Object.assign({ token: localStorage.token },config.data || {}) 
  return config
}, error => {
  console.log(error)
  return Promise.reject(error)
})

instance.interceptors.response.use(response => {
  // 对响应进行处理，例如处理错误码等
  return response.data
}, error => {
  console.log(error)
  return Promise.reject(error)
})

export default instance
Vue.prototype.http = instance